﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Cityship
{


    class Ship
    {
        private string _name;
        public string Name { get { return _name; } }

        private ShipStructure _structure;
        public ShipStructure Structure { get { return _structure; } }

        private List<Entity> _entities;

        private List<Entity> _deadEntities;
        public List<Entity> DeadEntities { get { return _deadEntities; } }

        public Ship(string shipName)
        {
            _name = shipName;
            _structure = new ShipStructure();
            _entities = new List<Entity>();
            _deadEntities = new List<Entity>();
            AddEntity("l");
        }

        public Deck GetDeckDeadEntities(int deckIndex)
        {
            Deck deck = _structure.GetDeck(deckIndex);
            if (deck != null)
            {
                return deck;
            }
            return _structure.DefaultDeck;
        }

        public Deck GetDeckByIndex(int currentDeckIndex)
        {
            return _structure.GetDeck(currentDeckIndex);
        }

        public void AddToDyingEntities(Entity entity)
        {
                _deadEntities.Add(entity);
        }

        public void RemoveEntity(Entity entity)
        {
            if (_entities.Contains(entity))
            {
                _entities.Remove(entity);
            }
            if (_deadEntities.Contains(entity))
            {
                _deadEntities.Remove(entity);
            }
        }

        public void AddEntity(string newEntityName)
        {
            for (int x = 0; x < 200; x++)
            {
                _entities.Add(new Entity(this, newEntityName+x.ToString().PadRight(5), new Point(0, 0),getRandomDeck()));
            }
        }

        private Deck getRandomDeck()
        {
            return GetDeckByIndex(Utility.rand.Next(0, _structure.DeckCount));
        }

        public void UpdateEntities(GameTime gameTime)
        {
            KeyboardState k_state = Keyboard.GetState();
            for (int x = 0; x < _entities.Count; x++)
            {
                Entity entity = _entities[x];
                entity.Update(k_state);
            }
            for (int x = _deadEntities.Count - 1; x >= 0; x--)
            {
                if (_deadEntities[x].CheckDecayed(gameTime))
                {
                    _deadEntities[x].Die();
                }
            }

            _structure.GetDeck(0).ClearPressureChecked();
            
        }
    }

    class ShipStructure
    {
        private List<Deck> _decks;
        public Deck DefaultDeck {get{return _decks[0];}}
        public int DeckCount { get { return _decks.Count; } }

        public Deck GetDeck(int deckIndex)
        {
            if (deckIndex < _decks.Count)
                return _decks[deckIndex];
            else
                return DefaultDeck;
        }
        public ShipStructure()
        {
            _decks = new List<Deck> { };
            _decks.Add(ShipBuilder.CreateDeck("structures\\large\\large0"));
            _decks.Add(ShipBuilder.CreateDeck("structures\\large\\large1"));
        }
        public void ClearDeck(int deckIndex)
        {
            _decks[deckIndex].Clear();
        }
        public void OutlineDeck(int deckIndex)
        {
            _decks[deckIndex].Outline();
        }
    }
}
